//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using Microsoft.Research.ScientificWorkflow.TridentUtilities;
using SR = Microsoft.Research.DataLayer;
using Microsoft.Research.ScientificWorkflow.TridentModel;
using System.Globalization;


namespace Microsoft.Research.ScientificWorkflow.ManagementStudioModels
{
    /// <summary>
    /// A view friendly model of the Job object.
    /// </summary>
    public class JobViewModel
    {
        /// <summary>
        /// The job id.
        /// </summary>
        private Guid jobId;

        /// <summary>
        /// The ID of the workflow which the job represents.
        /// </summary>
        private Guid workflowId;

        /// <summary>
        /// The ID of the node where the job will be executed.
        /// </summary>
        private Guid nodeId;

        /// <summary>
        /// The name of the job.
        /// </summary>
        private string jobName;

        /// <summary>
        /// The user who created the job.
        /// </summary>
        private string createdBy;

        /// <summary>
        /// The user on behalf of whom the job was created.
        /// </summary>
        private string scheduledBy;

        /// <summary>
        /// The Id of the user on behalf of whom the job was created.
        /// </summary>
        private Guid scheduledID;

        /// <summary>
        /// The name of the workflow which the job represents.
        /// </summary>
        private string workflowName;

        /// <summary>
        /// The time at which the job was started.
        /// </summary>
        private DateTime startTime;

        /// <summary>
        /// The time at which the job completed.
        /// </summary>
        private DateTime completedTime;

        /// <summary>
        /// The description.
        /// </summary>
        private string description;

        /// <summary>
        /// The current job status.
        /// </summary>
        private SR.JobStatus status;

        /// <summary>
        /// Machine in which the job is sceduled to execute.
        /// </summary>
        private NodeViewModel machineScheduled;

        /// <summary>
        /// 
        /// </summary>
        public JobViewModel()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Job"></param>
        public JobViewModel(SR.Job job)
        {
            if (job == null)
            {
                throw new ArgumentNullException("job");
            }

            this.jobId = ((SR.IObject)job).ID;
            this.jobName = job.Name;
            this.startTime = job.StartTime;
            this.completedTime = job.StopTime;
            this.createdBy = job.Created.User;
            this.scheduledBy = job.ScheduledBy.Name;

            if (job.Machines.Count > 0)
            {
                this.machineScheduled = new NodeViewModel(job.Machines[0]);
            }

            SR.Activity workflow = job.ActivityInstances[0].Activity;
            this.workflowName = workflow.DisplayName;
            this.workflowId = ((SR.IObject)workflow).ID;


            if (job.Status == SR.JobStatus.StartPending)
            {
                this.description = ManagementStudioResourceManager.GetString("ExecutionPendingMessage");
            }
            else if (job.Status == SR.JobStatus.Running)
            {
                this.description = ManagementStudioResourceManager.GetString("ExecutionInProgressMessage");
            }
            else if (job.ErrorMessage != null && !job.ErrorMessage.Equals(string.Empty))
            {
                this.description = job.ErrorMessage;
            }

            this.status = job.Status;
        }

        /// <summary>
        /// The job id.
        /// </summary>
        public Guid JobId
        {
            get
            {
                return jobId;
            }
        }

        /// <summary>
        /// The ID of the workflow which the job represents.
        /// </summary>
        public Guid WorkflowId
        {
            get
            {
                return workflowId;
            }
            set
            {
                workflowId = value;
            }
        }

        /// <summary>
        /// The ID of the node where the job will be executed.
        /// </summary>
        public Guid NodeId
        {
            get
            {
                return nodeId;
            }
            set
            {
                nodeId = value;
            }
        }

        /// <summary>
        /// The Id of the user on behalf of whom the job was created.
        /// </summary>
        public Guid ScheduledById
        {
            get
            {
                return this.scheduledID;
            }
            set
            {
                this.scheduledID = value;
            }
        }

        /// <summary>
        /// The name of the job.
        /// </summary>
        public string JobName
        {
            get
            {
                return jobName;
            }
            set
            {
                jobName = value;
            }
        }

        /// <summary>
        /// The node name.
        /// </summary>
        public string NodeName
        {
            get
            {
                return (MachineScheduled != null) ? MachineScheduled.Name : string.Empty;
            }
        }

        /// <summary>
        /// The user who created the job.
        /// </summary>
        public string CreatedBy
        {
            get
            {
                return createdBy;
            }
            set
            {
                createdBy = value;
            }
        }

        /// <summary>
        /// The user on behalf of whom the job was created.
        /// </summary>
        public string ScheduledBy
        {
            get
            {
                return scheduledBy;
            }
            set
            {
                scheduledBy = value;
            }
        }

        /// <summary>
        /// The name of the workflow which the job represents.
        /// </summary>
        public string WorkflowName
        {
            get
            {
                return workflowName;
            }
            set
            {
                workflowName = value;
            }
        }

        /// <summary>
        /// The time at which the job was started.
        /// </summary>
        public DateTime StartTime
        {
            get
            {
                return startTime;
            }
            set
            {
                startTime = value;
            }
        }

        /// <summary>
        /// The time at which the job completed.
        /// </summary>
        public DateTime CompletedTime
        {
            get
            {
                DateTime result = completedTime;
                if (startTime.Equals(DateTime.MinValue))
                {
                    result = DateTime.MinValue;
                }
                return result;
            }
            set
            {
                completedTime = value;
            }
        }

        /// <summary>
        /// The description.
        /// </summary>
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        /// <summary>
        /// The current status of the workflow.
        /// </summary>
        public SR.JobStatus Status
        {
            get
            {
                return this.status;
            }
            set
            {
                this.status = value;
            }
        }

        /// <summary>
        /// Gets or sets the machine in which the job is sceduled to execute.
        /// </summary>
        public NodeViewModel MachineScheduled
        {
            get
            {
                return this.machineScheduled;
            }
        }

        /// <summary>
        /// Overridden tostring method. For automation Ids.
        /// </summary>
        /// <returns>The string representation of the object.</returns>
        public override string ToString()
        {
            if (!string.IsNullOrEmpty(this.jobName))
            {
                return this.jobName;
            }
            return base.ToString();
        }
    }
}
